package mapkit.layers
{
	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.net.URLRequest;

	import mapkit.carto.Envelope;
	import mapkit.carto.IMap;
	import mapkit.carto.Location;

	import mx.core.UIComponent;

	public class MapImage extends UIComponent implements IMap
	{
		private var _extent:Envelope=new Envelope();

		final public function get extent():Envelope
		{
			return _extent;
		}

		final public function set extent(value:Envelope):void
		{
			_extent=value;
		}

		final public function get center():Location
		{
			return extent.center;
		}

		final public function get scale():Number
		{
			return width / extent.width;
		}

		private var _url:String="";

		final public function get url():String
		{
			return _url;
		}

		final public function set url(value:String):void
		{
			_url=value;
		}

		private var _request:URLRequest=null;

		private var _loader:Loader=null;

		override protected function createChildren():void
		{
			super.createChildren();

			_loader=new Loader();
			_request=new URLRequest();
			addChild(_loader);
			cacheAsBitmap=true;
		}

		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
		{

			graphics.clear();
			graphics.lineStyle(2, 0xff0000);
			graphics.drawRect(0, 0, width, height);

			try
			{
				(_loader.contentLoaderInfo.content as Bitmap).bitmapData.dispose();
			}
			catch (e:*)
			{
				//IGNORE
			}
			_loader.unload();
			updateRequest(_request);
			_loader.load(_request);
		}

		protected virtual function updateRequest(request_:URLRequest):void
		{
			//TO BE IMPLEMENTED IN SUBCLASSES
		}

	}
}